<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


/**
 * 断点输出
 * @date 2020年1月17日17:40:19
 * @param $data
 */
function echoJson( $data)
{
    echo json_encode( $data, JSON_UNESCAPED_UNICODE);exit;
}

/**
 * 生成GUID
 * @date 2020年1月17日17:40:23
 * @return mixed|string [36位字符串]
 */
function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45);
    $uuid = chr(123)
        .substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12)
        .chr(125);
    $uuid = str_replace(array('{','}'), array('',''), $uuid);
    return $uuid;
}


/**
 * md5密码加密
 * @date 2020年1月17日17:40:31
 *  [ɪnˈkrɪpt] encrypt
 * @param $password string 密码
 * @param string $salt 密码盐
 * @return string
 */
function encryptedPassword( $password, $salt = '')
{
    return md5( md5( $password) . $salt);
}

/**
 * 获取ip地址
 * @date 2020年1月17日17:41:03
 * @return array|false|string
 */
function get_ip()
{
    //判断服务器是否允许$_SERVER
    if(isset($_SERVER)){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }else{
            $realip = $_SERVER['REMOTE_ADDR'];
        }
    }else{
        //不允许就使用getenv获取
        if(getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv( "HTTP_X_FORWARDED_FOR");
        }elseif(getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        }else{
            $realip = getenv("REMOTE_ADDR");
        }
    }

    return $realip;
}

/**
 * 获取 IP  地理位置
 * 淘宝IP接口
 * @date 2020年1月17日17:41:11
 * @param string $ip
 * @return string
 */
function getCity($ip = '')
{


    $url="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=$ip&co=&resource_id=6006&t=1545033805778&ie=utf8&oe=utf8&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery1102001483456117003068_1545033228404&_=1545033228445";

//    $rdata = file_get_contents($url);
    $rdata = useFileGet($url);
    $rdata = strstr($rdata,"{");
    $rdata = strrev(strstr(strrev($rdata),"}"));
    $rdata =json_decode($rdata,true);

    return isset($rdata['data'][0]['location'])?$rdata['data'][0]['location']:'當前IP無法定位位置';

}

/**
 *  使用file_get_contens
 */
function useFileGet($url){
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $contents = curl_exec($ch);
    curl_close($ch);

    return $contents;
}










































